package workforce.limousine.struts.core;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;

import limousine.dbservice.ImageService;
import limousine.util.SelectItem;
import limousine.util.Service;
import limousine.util.Token;
import limousine.vo.Image;
import limousine.vo.UploadImage;
import limousine.vo.Users;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class UploadImageInit extends Action {

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        HttpSession session = request.getSession();
        System.out.println("***UploadImageInit");

        Users user = (Users) session.getAttribute(Token.USER_SESSION);
        if (user == null) {
            System.out.println("--SESSION EXPIRE--");
            return mapping.findForward(Token.SESSION_EXPIRE);
        }
        /* if (!user.getRole_name().equals("Admin")) {
        System.out.println("--Not Admin Hack--");
        return mapping.findForward(Token.SESSION_EXPIRE);
        }*/

        //        DataSource ds = this.getDataSource(request, Token.DATASOURCE_NAME);
        Service service = new Service();
        DataSource ds = service.getDataSource();
        service.removeMsgAttribute(request, session, "");

        session.removeAttribute(Token.UPLOAD_IMAGE);
        session.removeAttribute(Token.UPLOAD_IMAGE_CATEGORY_LIST);
        session.removeAttribute(Token.UPLOAD_IMAGE_TYPE_LIST);



        String catId = request.getParameter("catId");
        String catLock = request.getParameter("catLock");        
        
        UploadImage f = (UploadImage) form;
        f.setId("");
        f.setName("");
        f.setDetail("");        
        f.setCreateDate("");
        f.setEnable("");
        f.setLastUpdate("");
        f.setTitle("");
        f.setType("");
        f.setUpdateBy(user.getId());
        f.setTbRefId(session.getId());
        if (catId != null) {
            System.out.println("cateId["+catId+"]");
            f.setCategory(catId);            
        }
        if(catLock != null && catLock.equals("1")){
            System.out.println("catLock["+catLock+"]");
            f.setDisable(true);    
        }
        
        List<SelectItem> sl = service.getUploadImageTableIdList(f.getCategory(),true);
        if (sl.size() > 0) {
            session.setAttribute(Token.UPLOAD_IMAGE_CATEGORY_LIST, sl);
        }
        

        List<SelectItem> typeList = service.getUploadImageTypeList(f.getType());
        System.out.println("size typeList" + typeList.size());
        if (typeList.size() > 0) {
            session.setAttribute(Token.UPLOAD_IMAGE_TYPE_LIST, typeList);
        }

        ImageService is = new ImageService();
        is.setDataSource(ds);

        List<Image> listImage = is.getIdByCategoryTbRefId(f.getCategory(),session.getId());
        if(listImage.size() > 0){
            session.setAttribute(Token.UPLOAD_IMAGE,listImage);
        }

        return mapping.findForward(Token.SUCCESS);

    }
}
